<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>C# Winform手记 | 没想好的个人博客</title>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<!-- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> -->
   <!-- Font Awesome CSS-->
   <link href="https://cdn.bootcss.com/font-awesome/5.13.0/css/all.css" rel="stylesheet">

   <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.7.2/css/all.min.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<link rel="shortcut icon" href="https://shixixiyue.gitee.io//favicon.ico?v=1709791195950">
<link rel="stylesheet" href="https://shixixiyue.gitee.io//styles/main.css">



<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<script src="https://jsd.onmicrosoft.cn/npm/vue/dist/vue.js"></script>


<style>
  .hljs-ln{
    padding-bottom: 1.2rem;
    margin: 0rem !important;
  }
  .hljs-ln td {
    border: none !important;
    padding: 2px 6px !important;
    line-height: 1.2rem;
    font-size: 1rem;
  }
  .hljs-ln tr td:first-child  {
    color: cadetblue;
    border-right: 1px solid !important;
  }
  .hljs-ln tr {
    border: none !important;
  }
</style>

    <meta name="description" content="刚写了个winform的安装程序，在此做下记录








导言
1. 及时处理
2. 资源嵌入和使用
3. 解压
4. 运行bat









导言
刚写了个winform的安装程序，在此做下记录

1. 及时处理
挺常用的，以..." />
    <meta name="keywords" content="其他" />
  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://shixixiyue.gitee.io/">
        <img src="https://shixixiyue.gitee.io//images/avatar.png?v=1709791195950" class="site-logo">
        <h1 class="site-title">没想好的个人博客</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/4gOKry8Qv/" class="site-nav">
            .Net Core
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/njETBpqXu/" class="site-nav">
            FineUICore
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/XyXyeLOBB" class="site-nav">
            读书笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/TyVX6hfCJ" class="site-nav">
            Vue笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/3Hz2sPtzO/" class="site-nav">
            产品
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/Db1wShB_E" class="site-nav">
            其他
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            一一一一一一一一一
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//archives" class="site-nav">
            文章归档
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tags" class="site-nav">
            全部标签
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      没事啥都说
    </div>
    <div class="site-footer">
      QQ：<a href="tencent://message/?uin=935732994&Site=''&Menu=yes">935732994<a/> <a href="http://beian.miit.gov.cn">京ICP备2024045283号<a/> | <a class="rss" href="https://shixixiyue.gitee.io//atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">C# Winform手记</h2>
            <div class="post-date">2019-06-12
            
                
                  <a href="https://shixixiyue.gitee.io/tag/Db1wShB_E/" class="tag">
                    其他
                  </a>
                
            </div>
            
            <div class="post-content" v-pre>
              <p>刚写了个winform的安装程序，在此做下记录</p>
<!-- more -->
<p><ul class="markdownIt-TOC">
<li>
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#%E5%AF%BC%E8%A8%80">导言</a></li>
<li><a href="#1-%E5%8F%8A%E6%97%B6%E5%A4%84%E7%90%86">1. 及时处理</a></li>
<li><a href="#2-%E8%B5%84%E6%BA%90%E5%B5%8C%E5%85%A5%E5%92%8C%E4%BD%BF%E7%94%A8">2. 资源嵌入和使用</a></li>
<li><a href="#3-%E8%A7%A3%E5%8E%8B">3. 解压</a></li>
<li><a href="#4-%E8%BF%90%E8%A1%8Cbat">4. 运行bat</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</p>
<blockquote>
<h4 id="导言">导言</h4>
<p>刚写了个winform的安装程序，在此做下记录</p>
</blockquote>
<h4 id="1-及时处理">1. 及时处理</h4>
<p>挺常用的，以下来自引用：</p>
<p>因为winform的消息循环是一个线程来处理，那么假如你的某个操作比较耗时，那么消息处理得等你这个耗时操作做完了才能继续，而Application.DoEvents方法就是允许你在耗时操作的内部调用它，而去处理消息队列中的消息。</p>
<pre><code>Application.DoEvents
</code></pre>
<h4 id="2-资源嵌入和使用">2. 资源嵌入和使用</h4>
<p>因为我想着最后生成的是一个大exe，里面用到的包通过嵌入资源的方式都生成到exe中了（exe的文件会很大），用的时候再拿出来</p>
<figure data-type="image" tabindex="1"><img src="https://shixixiyue.gitee.io//post-images/1684479164938.png" alt="嵌入资源" loading="lazy"></figure>
<p>用的时候通过读取资源文件流，保存到临时文件，安装完成后再删除</p>
<pre><code>        //获得正在运行类所在的名称空间
        Type type = MethodBase.GetCurrentMethod().DeclaringType;
        string _namespace = type.Namespace;
        //临时文件
        string folderPath = Application.StartupPath + @&quot;\临时文件res&quot;;
        Directory.CreateDirectory(folderPath);
        //获得当前运行的Assembly
        Assembly _assembly = Assembly.GetExecutingAssembly();

        //文件名
        string name = &quot;activemq.zip&quot;;
        //根据名称空间和文件名生成资源名称
        string resourceName = _namespace + &quot;.res.&quot;+ name;
        //根据资源名称从Assembly中获取此资源的Stream
        Stream stream = _assembly.GetManifestResourceStream(resourceName);
        //全部的资源名
        //string[] resNames = _assembly.GetManifestResourceNames();
        byte[] srcBuf = new Byte[stream.Length];
        stream.Read(srcBuf, 0, srcBuf.Length);
        stream.Seek(0, SeekOrigin.Begin);
        //保存文件到本地
        try
        {
            using (FileStream fs = new FileStream(folderPath+ @&quot;\&quot;+ name, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fs.Write(srcBuf, 0, srcBuf.Length);
                fs.Close();
            }
        }
        catch (Exception ex)
        {
        }
</code></pre>
<p>删除</p>
<pre><code>DirectoryInfo subdir = new DirectoryInfo(Application.StartupPath + @&quot;\临时文件res&quot;);
subdir.Delete(true);          //删除子目录和文件
</code></pre>
<h4 id="3-解压">3. 解压</h4>
<p>jar包利用代码解压后文件损坏了……，网上找了好几个，终于有一个是可以用的，可以做到无损。还是用的 ICSharpCode.SharpZipLib.dll 也作为资源嵌入了</p>
<pre><code>    ZipHelper.UnpackFiles(Application.StartupPath + &quot;\\临时文件res\\activemq.zip&quot;, @&quot;c:\MQ&quot;);//解压

    jar uf更新
    
    jar xf解压
</code></pre>
<blockquote></blockquote>
<pre><code>    /// &lt;summary&gt;
    /// 解压缩文件
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;zipFile&quot;&gt;待解压缩的文件&lt;/param&gt;
    /// &lt;param name=&quot;directory&quot;&gt;解压缩后文件存放的目录&lt;/param&gt;
    public static bool UnpackFiles(string zipFile, string directory)
    {
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);

        ZipInputStream stream = new ZipInputStream(File.OpenRead(zipFile));
        ZipEntry theEntry = null;
        while ((theEntry = stream.GetNextEntry()) != null)
        {
            string directoryName = Path.GetDirectoryName(theEntry.Name);
            string fileName = Path.GetFileName(theEntry.Name);
            if (directoryName != string.Empty)
                Directory.CreateDirectory(directory + &quot;\\&quot; + directoryName);

            if (fileName != string.Empty)
            {
                FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
                int size = 2048;
                byte[] data = new byte[size];
                while (true)
                {
                    size = stream.Read(data, 0, data.Length);
                    if (size &gt; 0)
                        streamWriter.Write(data, 0, size);
                    else
                        break;
                }
                streamWriter.Close();
            }
        }
        stream.Close();
        return true;
    }
</code></pre>
<h4 id="4-运行bat">4. 运行bat</h4>
<p>启动MQ时候直接运行的bat</p>
<pre><code>    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.WorkingDirectory = @&quot;c:\MQ\bin\win64&quot;; 
    proc.StartInfo.FileName = &quot;activemq.bat&quot;;
    proc.StartInfo.Arguments = string.Format(&quot;10&quot;);//this is argument
    proc.StartInfo.CreateNoWindow = false;
    //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示，经实践可行
    proc.Start();
</code></pre>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://shixixiyue.gitee.io/tag/Db1wShB_E/" class="tag">
                    其他
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://shixixiyue.gitee.io/my-appbox-v20/">
                  <h3 class="post-title">
                    My AppBox V2.0
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script type="application/javascript">

AOS.init();

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  <script src="//jsd.onmicrosoft.cn/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js"></script>
  <script>
    hljs.initHighlightingOnLoad();
    //hljs.registerLanguage('mermaid', window.hljsDefineMermaid);
  </script>
  <script src="//jsd.onmicrosoft.cn/npm/highlightjs-line-numbers.js@2.8.0/dist/highlightjs-line-numbers.min.js"></script>
  <script>
    hljs.initLineNumbersOnLoad({
      singleLine: true
    });
  </script>
 <script src="https://jsd.onmicrosoft.cn/npm/mermaid/dist/mermaid.min.js"></script>
   <script>
    mermaid.initialize({
      startOnLoad: true,
      securityLevel: 'loose',
    });
  </script>





  </body>
</html>
